package com.rwy.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rwy.util.WebUtil;
import com.rwy.web.filter.authorize.AuthorizeStatus;

import lombok.extern.log4j.Log4j;

@ControllerAdvice
@Log4j
public class GlobalExceptionHandler {
	 
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public AuthorizeStatus jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
	log.error("url:" +req.getRequestURI() + " req param:" +  WebUtil.getParameters(req).toString());
	log.error(responseStack(e.getStackTrace()));
	log.error(e.getMessage());
	return new AuthorizeStatus("400", req.getRequestURI(), "bad request");
	}
	
	
	private String responseStack(StackTraceElement[] stack) {
		StringBuffer sb = new StringBuffer();
		if (stack != null) {
			for(int i =0 ; i < stack.length ; i++) {
				sb.append(stack[i].toString()+"; ");
			}
		}
		return sb.toString();
	}
}
